package com.ruoyi.web.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.entity.businessentity.Business;
import com.ruoyi.common.mapper.BusinessMapper;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.web.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 查询业务
     * @param business
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<Business> query(Business business) {
        List<Business> displayList = new ArrayList<>();
        List<Business> query = businessMapper.query(business);
        for (int i = 0; i < query.size(); i++) {
            if (query.get(i).getDeptName()!=null){
                displayList.add(query.get(i));
            }
        }
        return displayList;
    }

    /**
     * 新增业务
     * @param business
     * @return
     */
    @Transactional
    @Override
    public int add(Business business) {
        /** 业务表插入数据
         * 前端参数 businessNumber businessName businessIntroduce deptId
         * 后台增加 id createBy createTime
         * */
        try {
            String businessId = IdUtils.simpleUUID();
            business.setId(businessId);
            business.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            business.setCreateTime(new Date());
            business.setStatus("0");
            int i1 = businessMapper.addBusiness(business);

            /** 业务_部门表插入数据 */
            business.setId(IdUtils.simpleUUID());
            business.setBusinessId(businessId);
            int i2 = businessMapper.addBusinessDept(business);
            if (i1==1&&i2==1)
                return 1;
            else
                return 0;
        }catch (Exception e){
            LogUtils.getBlock(e);
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 修改业务
     * @param business
     * @return
     */
    @Override
    public int editBusiness(Business business) {
        try {
            business.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            business.setUpdateTime(new Date());
            int i = businessMapper.editBusiness(business);
            if (i==1)
                return 1;
            else
                return 0;
        }catch (Exception e){
            LogUtils.getBlock(e);
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 删除业务
     * @param id
     * @return
     */
    @Override
    public int deleteBusiness(String id) {
        try {
            return businessMapper.deleteBusiness(id);
        }catch (Exception e){
            LogUtils.getBlock(e);
            e.printStackTrace();
            return 0;
        }
    }
}
